package com.aaa.forum.services.impl;

import com.aaa.forum.common.AppResult;
import com.aaa.forum.common.ResultCode;
import com.aaa.forum.dao.ArticleLikeMapper;
import com.aaa.forum.exception.ApplicationException;
import com.aaa.forum.model.ArticleLike;
import com.aaa.forum.services.IArticleLikeService;
import com.aaa.forum.services.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Slf4j
@Service
public class ArticleLikeServiceImpl implements IArticleLikeService {
    @Resource
    private ArticleLikeMapper articleLikeMapper;

    @Resource
    private IArticleService articleService;

    @Override
    public void like(Long userId, Long articleId) {
        if (userId == null || userId <= 0 || articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询点赞记录是否存在
        ArticleLike articleLike = articleLikeMapper.selectByUserIdAndArticleId(userId, articleId);
        int row = 0;
        if (articleLike == null) {
            // 点赞记录为空, 创建一条新记录
            articleLike = new ArticleLike();
            articleLike.setUserId(userId);
            articleLike.setArticleId(articleId);
            articleLike.setState((byte) 1);
            articleLike.setDeleteState((byte) 0);
            Date date = new Date();
            articleLike.setUpdateTime(date);
            articleLike.setCreateTime(date);
            row = articleLikeMapper.insertSelective(articleLike);

            // 更新帖子点赞数量
            articleService.addOneLikeCountById(articleId);
        } else {
            if (articleLike.getState() == 1 && articleLike.getDeleteState() == 0) {
                // 记录未被删除，且状态为点赞状态
                return;
            }
            ArticleLike updateObject = new ArticleLike();
            updateObject.setId(articleLike.getId());
            updateObject.setState((byte) 1);
            updateObject.setDeleteState((byte) 0);
            updateObject.setUpdateTime(new Date());
            row = articleLikeMapper.updateByPrimaryKeySelective(updateObject);
            // 更新帖子点赞数量
            articleService.addOneLikeCountById(articleId);
        }
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void cancel(Long userId, Long articleId) {
        if (userId == null || userId <= 0 || articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 查询点赞记录是否存在
        ArticleLike articleLike = articleLikeMapper.selectByUserIdAndArticleId(userId, articleId);
        if (articleLike == null || articleLike.getDeleteState() == 1 || articleLike.getState() == 0) {
            /*
                记录不存在, 无法取消点赞
                或者已经取消点赞了
             */
            return;
        }
        // 构造更新对象
        ArticleLike updateObject = new ArticleLike();
        updateObject.setId(articleLike.getId());
        updateObject.setState((byte) 0);
        updateObject.setUpdateTime(new Date());
        int row = articleLikeMapper.updateByPrimaryKeySelective(updateObject);
        if (row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        articleService.subOneLikeCountById(articleId);
    }

    @Override
    public Byte getState(Long userId, Long articleId) {
        if (userId == null || userId <= 0 || articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return articleLikeMapper.getState(userId, articleId);
    }
}
